package com.yoo.ons.base.adpater

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.chad.library.adapter4.BaseQuickAdapter

abstract class CustomBaseQuickAdapter<T : Any,V : ViewBinding>(val inflaterBlock: (LayoutInflater) -> V) :
    BaseQuickAdapter<T, CustomBaseQuickAdapter.VH<V>>() {
    class VH<V>(val binding: V) : RecyclerView.ViewHolder((binding as ViewBinding).root)

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH<V> {
        return VH(inflaterBlock(LayoutInflater.from(this.context)))
    }

    abstract fun onBindView(holder: VH<V>, position: Int, item: T?)

    override fun onBindViewHolder(holder: VH<V>, position: Int, item: T?) {
        val params: FrameLayout.LayoutParams = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        holder.binding.root.layoutParams = params
        onBindView(holder, position, item)
    }
}